package common

type ErrCode struct {
	Code    int
	Message string
}

const (
	ErrGeneralError int = iota + 10001
	ErrInvalidParam
	ErrDatabaseError
)

var ErrMsgMap map[int]string

func InitErrMsg() {
	ErrMsgMap = make(map[int]string)

	ErrMsgMap[ErrGeneralError] =
		"general error"
	ErrMsgMap[ErrInvalidParam] =
		"invalid parameters"
	ErrMsgMap[ErrDatabaseError] =
		"database error"
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func (e *ErrCode) Error() string {
	return e.Message
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func CreateErrCode(code int, message string) error {
	return &ErrCode{Code: code, Message: message}
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func NewErrCode(code int) error {
	if msg, ok := ErrMsgMap[code]; ok {
		return &ErrCode{Code: code, Message: msg}
	} else {
		return &ErrCode{Code: code, Message: "unknown error"}
	}
}
